﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using WebServer.DBUtility;
using WebServer.Models;

namespace WebServer.DBAccess
{
    public class FeedbackDA
    {
        #region 意见反馈
        /// <summary>
        /// 意见反馈列表
        /// </summary>
        /// <param name="PageSize"></param>
        /// <param name="Page"></param>
        /// <param name="Userid"></param>
        /// <param name="Come"></param>
        /// <param name="Type"></param>
        /// <param name="Text"></param>     
        /// <param name="Order">默认根据时间排序，1-ID，2-用户，3-类型，4，来源</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public static List<UFeedback> FeedbackSearch(int PageSize, int Page, int UserID, string Come, string Type, string Text, int Order, bool Desc, int IsDel)
        {
            List<UFeedback> entitys = new List<UFeedback>();
            try
            {
                SQLHelper sqlHelp = new SQLHelper();
                object[] tiaoj = new object[] { PageSize, (Page - 1) * PageSize, UserID, Come, Type, Text, Order, Desc, IsDel };
                using (IDataReader reader = sqlHelp.ExecuteReader("[sp_U_Feedback_Q]", tiaoj))
                {
                    UFeedback entity = null;
                    while (reader.Read())
                    {
                        entity = new UFeedback();
                        if (reader[0] != DBNull.Value)
                            entity.ID = reader[0].ToInt32();
                        else
                            entity.ID = 0;
                        if (reader[1] != DBNull.Value)
                            entity.UserID = reader[1].ToInt32();
                        else
                            entity.UserID = 0;
                        entity.Come = reader[2].ToString();
                        entity.Type = reader[3].ToString();
                        if (reader[4] != DBNull.Value)
                            entity.CreateTime = reader[4].ToDateTime();
                        else
                            entity.CreateTime = DateTime.Now;                     
                        entity.Text = reader[5].ToString();                       
                        entitys.Add(entity);
                    }

                    reader.Close();
                }
            }
            catch (Exception ex)
            {
            }

            return entitys;
        }



        /// <summary>
        /// 意见反馈列表总数
        /// </summary>    
        /// <param name="Userid"></param>
        /// <param name="Come"></param>
        /// <param name="Type"></param>
        /// <param name="Text"></param>            
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public static int FeedbackCount(int UserID, string Come, string Type, string Text, int IsDel)
        {
            try
            {
                SQLHelper sqlHelp = new SQLHelper();
                object[] tiaoj = new object[] { UserID, Come, Type, Text, IsDel };
                object obj = sqlHelp.ExecuteSPScalar("[sp_U_Feedback_MC]", tiaoj);
                if (obj != null && obj != DBNull.Value)
                {
                    return Convert.ToInt32(obj);
                }
                else
                    return 0;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }


        /// <summary>
        /// 删除意见反馈
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static int DelFeedback(int ID)
        {
            try
            {
                SQLHelper sqlHelp = new SQLHelper();
                object obj = sqlHelp.ExecuteSPScalar("[sp_U_Feedback_D]", ID);
                if (obj != null && obj != DBNull.Value)
                {
                    return Convert.ToInt32(obj);
                }
                else
                    return -1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        /// <summary>
        /// 新增或者修改意见反馈
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static int AddOrUpFeedback(UFeedback model)
        {
            try
            {
                SQLHelper sqlHelp = new SQLHelper();
                object[] WhereInfo = new object[] { model.ID, model.UserID, model.Come, model.Type, model.Text };

                object obj = sqlHelp.ExecuteSPScalar("[sp_U_Feedback_Set]", WhereInfo);
                if (obj != null && obj != DBNull.Value)
                {
                    return Convert.ToInt32(obj);
                }
                else
                    return -1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        /// <summary>
        /// 根据ID获取意见反馈
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static UFeedback FeedbackByID(int ID)
        {
            UFeedback entity = new UFeedback();
            try
            {

                SQLHelper sqlHelp = new SQLHelper();
                using (IDataReader reader = sqlHelp.ExecuteReader("[sp_U_Feedback_ByID]", ID))
                {
                    while (reader.Read())
                    {

                        entity = new UFeedback();
                        if (reader[0] != DBNull.Value)
                            entity.ID = reader[0].ToInt32();
                        else
                            entity.ID = 0;
                        if (reader[1] != DBNull.Value)
                            entity.UserID = reader[1].ToInt32();
                        else
                            entity.UserID = 0;
                        entity.Come = reader[2].ToString();
                        entity.Type = reader[3].ToString();
                        if (reader[4] != DBNull.Value)
                            entity.CreateTime = reader[4].ToDateTime();
                        else
                            entity.CreateTime = DateTime.Now;
                        entity.Text = reader[5].ToString();          
                    }

                    reader.Close();
                }

            }
            catch (Exception ex)
            {
            }
            return entity;
        }
        #endregion

    }
}
